package com.fimuz.mqmeet.api.roombook.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.roombook.dao.RoombookDao;
import com.fimuz.mqmeet.api.roombook.service.RoombookService;
import com.fimuz.mqmeet.api.roombook.service.model.CreateSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.CreateSrModel;
import com.fimuz.mqmeet.api.roombook.service.model.DeleteSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.DeleteSrModel;
import com.fimuz.mqmeet.api.roombook.service.model.GetBoardRoomSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.GetBoardRoomSrModel;
import com.fimuz.mqmeet.api.roombook.service.model.GetSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.GetSrModel;
import com.fimuz.mqmeet.api.roombook.service.model.ListBoardRoomSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.ListBoardRoomSrModel;
import com.fimuz.mqmeet.api.roombook.service.model.UpdateSpModel;
import com.fimuz.mqmeet.api.roombook.service.model.UpdateSrModel;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzBoardroomMapper;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzRoombookMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzBoardroom;
import com.fimuz.mqmeet.helper.mybatis.model.HxzRoombook;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.utils.ExtAsserts;
import com.fosun.spr.core.utils.SprDaoUtils;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class RoombookServiceImpl extends CoBaseService implements
		RoombookService {
	@Resource
	private RoombookDao roombookDao;
	@Resource
	private HxzRoombookMapper hxzRoombookMapper;
	@Resource
	private HxzBoardroomMapper hxzBoardroomMapper;

	@Override
	public ListBoardRoomSrModel listBoardRoom(ListBoardRoomSpModel spModel) {
		ListBoardRoomSrModel srModel = new ListBoardRoomSrModel();
		srModel.setDatas(roombookDao.listBoardRoom(spModel));
		// srModel.setTotalCount(roombookDao.listBoardRoomCount(spModel));
		return srModel;
	}

	@Override
	public GetBoardRoomSrModel getBoardRoom(GetBoardRoomSpModel spModel) {
		ExtAsserts.notNull(spModel.getBoardroomId(),"boardroomId");
		GetBoardRoomSrModel srModel = new GetBoardRoomSrModel();
		srModel.setDatas(roombookDao.getBoardRoom(spModel));
		return srModel;
	}

	@Override
	public UpdateSrModel update(UpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		ExtAsserts.notNull(spModel.getMeetTilte(),"meetTilte");
		ExtAsserts.notNull(spModel.getRoomId(),"roomId");
		ExtAsserts.notNull(spModel.getMeetTilte(),"meetTime");
		ExtAsserts.notNull(spModel.getDurationMins(),"durationMins");
	
		//判定roomId是否存在
		SprDaoUtils.assertExistsById(hxzBoardroomMapper, spModel.getRoomId());

		UpdateSrModel srModel = new UpdateSrModel();
		HxzRoombook record = (HxzRoombook)SprDaoUtils.getById(hxzRoombookMapper, spModel.getId());
		record.setMeetTime(getDate(spModel.getMeetTime()));
		record.setMeetTitle(spModel.getMeetTilte());
		SprDaoUtils.updateByPrimaryKey(hxzRoombookMapper, record);
		return srModel;
	}

	@Override
	public CreateSrModel create(CreateSpModel spModel) {
		ExtAsserts.notNull(spModel.getMeetTilte(),"meetTilte");
		ExtAsserts.notNull(spModel.getRoomId(),"roomId");
		ExtAsserts.notNull(spModel.getMeetTilte(),"meetTime");
		ExtAsserts.notNull(spModel.getDurationMins(),"durationMins");
		
		//判定roomId是否存在
		SprDaoUtils.assertExistsById(hxzBoardroomMapper, spModel.getRoomId());
		
		CreateSrModel srModel = new CreateSrModel();
		HxzRoombook record = new HxzRoombook();
		cglibCopyProperties(spModel, record, false);
		record.setMeetTime(getDate(spModel.getMeetTime()));
		record.setMeetTitle(spModel.getMeetTilte());
		record.setTeamId(spModel.getSessionTeamId());
		record.setLoginuserId(spModel.getSessionUserId());
		SprDaoUtils.insert(hxzRoombookMapper, record);
		srModel.setId(record.getId());
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		//判定roomId是否存在
		HxzRoombook record = (HxzRoombook)SprDaoUtils.getById(hxzRoombookMapper, spModel.getId());
		
		GetSrModel srModel = new GetSrModel();
		cglibCopyProperties(record, srModel, false);
		srModel.setMeetTilte(getDateString(record.getMeetTime()));
		return srModel;
	}

	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(), "id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprDaoUtils.delete(hxzRoombookMapper, spModel.getId());
		return srModel;
	}
}
